package demo.aspect;
import demo.model.Account;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class AccountAspect2 {

    @Pointcut("call(boolean demo.model.Account.pay(int)) && args(amount) && target(account)")
    public void callPay(int amount, Account account) {
    }

    @Around("callPay(amount,account)")
    public Object around(ProceedingJoinPoint pjp, int amount, Account account) throws Throwable {
        if (account.balance < amount) {
            System.out.println("[AccountAspect2]拒绝付款!");
            return false;
        }
        return pjp.proceed();
    }

    @Before("callPay(amount,account)")
    public void before(int amount, Account account) {
        System.out.println("[AccountAspect2]付款前总金额: " + account.balance);
        System.out.println("[AccountAspect2]需要付款: " + amount);
    }

    @After("callPay(amount,account)")
    public void after(int amount, Account account) {
        System.out.println("[AccountAspect2]付款后，剩余：" + account.balance);
    }
}